package org.phoenixframework.oauth2.server.entity;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.phoenixframework.common.entity.SysUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 *
 * @author doublelifeke
 * Email: hautxxxyzjk@163.com
 * DateTime: 2021/6/7 17:12
 * Description: 登录认证用户信息
 */
@Data
public class SecurityUser implements UserDetails {

    private Long id;

    @ApiModelProperty(value = "登录名")
    private String username;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "头像")
    private String avatar;

    @ApiModelProperty(value = "是否超级管理员，1/0")
    private Integer isSuper;

    @ApiModelProperty(value = "部门ID")
    private Long deptId;

    @ApiModelProperty(value = "用户状态")
    private Boolean enabled;

    @ApiModelProperty(value = "权限数据")
    private Collection<GrantedAuthority> authorities;

    public SecurityUser() {

    }

    public SecurityUser(SysUser sysUser, Collection<GrantedAuthority> authorities) {
        this.setId(sysUser.getId());
        this.setUsername(sysUser.getUsername());
        this.setPassword(sysUser.getPassword());
        this.setAvatar(sysUser.getAvatar());
        this.setIsSuper(sysUser.getIsSuper());
        this.setDeptId(sysUser.getDeptId());
        this.setEnabled(sysUser.getDelFlag() == 0);
        this.setAuthorities(authorities);
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
